package com.hnust.dao;

import com.hnust.domain.Question;
import com.hnust.domain.QuestionType;
import com.hnust.domain.record.SubjectData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.*;
import com.hnust.domain.record.AnswerObj;
import com.hnust.domain.record.SubjectDataRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * 题目表的DAO层
 * @author 42
 * @date 2020/10/29
 */

/**
 * @author gh
 * @date 2020/10/31
 */
@Mapper
@Repository
public interface QuestionDao {
    //获取题目列表s
    @Select("select que_id,content,cha_id,cou_id,level,analysis,point,que_typ_id,cou_tar_id,que_access from question where cou_id = #{course_id} limit #{currentPage},#{rows} ")
    @ResultType(Question.class)
    List<Question> getQuestionList(String course_id,int currentPage,int rows);


//    //查询题目
//    @Select("select que_id,content,cha_id,cou_id,level,analysis,point,que_typ_id,cou_tar_id,que_access from question where content like CONCAT(CONCAT('%', #{keyword}),'%') and que_typ_id = #{que_typ_id} limit #{now_page},7")
//    @ResultType(Question.class)
//    List<Question> queryQuestionList(String keyword,String que_typ_id,Integer now_page);

    @SelectProvider(type = queryQuestion.class, method = "queryQuestionList")
    @ResultType(Question.class)
    List<Question> queryQuestionList(String course_id,String keyword,Integer now_page,Integer rows);
    class queryQuestion{
        public String queryQuestionList(String course_id,String keyword,Integer now_page){
            String sql = "";
            if(keyword == null){
              sql = "select que_id,content,cha_id,cou_id,level,analysis,point,que_typ_id,cou_tar_id,que_access " +
                        "from question " +
                        "where cou_id=#{course_id} " +
                        "limit #{now_page},#{rows} ";
            }else{
               sql = "select que_id,content,cha_id,cou_id,level,analysis,point,que_typ_id,cou_tar_id,que_access " +
                        "from question " +
                        "where content like CONCAT(CONCAT('%', #{keyword}),'%')  and cou_id=#{course_id} " +
                        "limit #{now_page},#{rows} ";
            }
            return sql;
        }
    }

    //查询时题目总数
    @SelectProvider(type = queryQuestionCount.class, method = "queryQuestionList")
    @ResultType(Integer.class)
    Integer queryQuestionListCount(String keyword,String course_id);
    class queryQuestionCount{
        public String queryQuestionList(String keyword,String course_id){
            String sql = "";
            if(keyword == null){
                sql = "select count(*) from question where cou_id=#{course_id} ";
            }else{
                sql = "select count(*) from question where cou_id=#{course_id} and content like CONCAT(CONCAT('%', #{keyword}),'%')";
            }
            return sql;
        }
    }

    //获取题目总数
    @Select("select count(*) from question where cou_id = #{course_id}")
    @ResultType(Integer.class)
    Integer getQuestionListCount(String course_id);


    //删除题目对于备选答案
    @Delete("delete from alternative_answer where que_id = #{queId}")
    @ResultType(Integer.class)
    Integer deleteQuestionAnswer(String queId);
    //删除做题记录
    @Delete("delete from question_record where que_id = #{queId}")
    @ResultType(Integer.class)
    Integer deleteQuestionRecord(String queId);
    //删除试卷题题目
    @Delete("delete from question_paper where que_id = #{queId}")
    @ResultType(Integer.class)
    Integer deletePaperQuestion(String queId);
    //删除题目
    @Delete("delete from question where que_id = #{queId}")
    @ResultType(Integer.class)
    Integer deleteQuestion(String queId);

    //查询题目类型
    @Select("select que_typ_id from question where cou_id= #{course_id} group by que_typ_id")
    @ResultType(String.class)
    List<String> getCourseQuestionList(String course_id);
    //获取题型信息
    @Select("select que_typ_id,name from question_type where que_typ_id= #{que_typ_id}")
    @ResultType(QuestionType.class)
    QuestionType getCourseQuesTypeList(String que_typ_id);

    //修改题目
    @Update("update question set cou_id=#{course},cha_id=#{chapter},que_typ_id=#{type},level=#{difficult},content=#{content},analysis=#{resolve},point=#{knowledge_pointer},cou_tar_id=#{course_target},que_access=#{queAccess} where que_id=#{id}")
    @ResultType(Integer.class)
    Integer modifyQuestionById(String id, String content, String course, String chapter, String type, Integer difficult, String resolve, String knowledge_pointer, String course_target,Integer queAccess);

    //添加题目
    @Insert("insert into question(que_id,cou_id,cha_id,que_typ_id,level,content,analysis,point,cou_tar_id,que_access) values (#{Id},#{course},#{chapter},#{type},#{difficult},#{content},#{resolve},#{knowledge_pointer},#{course_target},#{queAccess})")
    @ResultType(Integer.class)
    Integer addQuestion(String Id,String content, String course, String chapter, String type, Integer difficult, String resolve, String knowledge_pointer, String course_target,Integer queAccess);

      /*
      * @Author Tonited
      * @Description //TODO 根据id获取题目
      * @Date 15:44 2020/10/31
      * @Param [questionId]
      * @return com.hnust.domain.Question
      **/
     @Select("select * from question where que_id=#{questionId}")
     @ResultType(Question.class)
     Question queryQuestionById(String questionId);

    /**
     * 获取某个课程，某个题型的所有题目
     * @param cou_id 课程id
     * @param que_type_id 问题id
     * @return SubjectData
     */
    @Select("select\n" +
            "question.que_id as id,\n" +
            "question.content as content,\n" +
            "question.cou_id as course_id,\n" +
            "question.cha_id as chapter_id,\n" +
            "question.que_typ_id as type,\n" +
            "question.level as difficult,\n" +
            "question.analysis as resolve,\n" +
            "question.point as knowledge_pointer,\n" +
            "question.cou_tar_id as course_target_id,\n" +
            "question.que_access as que_access\n" +
            "from question\n" +
            "where \n" +
            "question.cou_id = #{cou_id} \n" +
            "and\n" +
            "question.que_typ_id = #{que_type_id} limit 5 offset #{offset} ")
    @ResultType(SubjectData.class)
    List<SubjectData> getCourseQuesList(String cou_id, String que_type_id, int offset);

    /**
     * 获取某个课程的所有类型的题目
     * @param cou_id 课程id
     * @param que_type_id 题目类型id
     * @return 结果
     */
    @Select("select\n" +
            "question.que_id as id,\n" +
            "question.content as content,\n" +
            "question.cou_id as course_id,\n" +
            "question.cha_id as chapter_id,\n" +
            "question.que_typ_id as type,\n" +
            "question.level as difficult,\n" +
            "question.analysis as resolve,\n" +
            "question.point as knowledge_pointer,\n" +
            "question.cou_tar_id as course_target_id,\n" +
            "question.que_access as que_access\n" +
            "from question\n" +
            "where \n" +
            "question.cou_id = #{cou_id} and question.level = #{level} and question.que_typ_id=#{que_type_id}\n")
    @ResultType(SubjectData.class)
    List<SubjectData> getCourseQuesListAll(String cou_id, String que_type_id, int level);

    /**
     * 获取题目的答案列表
     * @param que_id 题目id
     * @return AnswerObj
     */
    @Select("select alternative_answer.content, alternative_answer.right\n" +
            "from alternative_answer \n" +
            "where que_id = #{que_id} ")
    @ResultType(AnswerObj.class)
    List<AnswerObj> getCourseQuesAnswerList(String que_id);

    /**
     * 获取题目总的数量
     * @param cou_id 课程id
     * @param que_type_id 问题id
     * @return 总数
     */
    @Select("select count(*) from question where question.cou_id = #{cou_id} and question.que_typ_id = #{que_type_id}")
    int getCourseQuesCount(String cou_id, String que_type_id);

    /**
     *  根据课程id，题目类型id，关键词来获取题目信息
     * @param colId 课程id
     * @param typeId 题目类型id
     * @param key 关键词
     * @param now 当前页数
     * @param rows 一页总数
     * @param diff 难度
     * @return 返回查找到的题目
     */
    @Select("select * from question where cou_id=#{colId} and level = #{diff} and que_typ_id=#{typeId} and content like concat('%',#{key},'%') limit #{now},#{rows}")
    @ResultType(Question.class)
    List<Question> searchQuestionByCon(String colId, String typeId, String key, Integer now, Integer rows,Integer diff);

    /*
     * @Author Tonited
     * @Description //TODO 获取searchQuestionByCon中不限制长度时能获取到的题目的总数
     * @Date 20:40 2020/12/15
     * @Param [colId, typeId, key, diff]
     * @return java.lang.Integer
     **/
    @Select("select count(*) from question where cou_id=#{colId} and level = #{diff} and que_typ_id=#{typeId} and content like concat('%',#{key},'%')")
    @ResultType(Integer.class)
    Integer countQuestionByCon(String colId, String typeId, String key, Integer diff);
    /**
     *  获取题目总数
     * @param colId 课程id
     * @param typeId 题目类型id
     * @param key 关键词
     * @return 题目总数
     */
    @Select("select count(*) from question where cou_id=#{colId} and que_typ_id=#{typeId} and content like concat('%',#{key},'%')")
    Integer getQuestionCountByCon(String colId, String typeId, String key);

    @Insert("insert into question(que_id,level,content,analysis,point,cou_id,cha_id,que_typ_id,cou_tar_id) values(#{que_id},#{level},#{content},#{analysis},#{point},#{cou_id},#{cha_id},#{que_typ_id},#{cou_tar_id})")
    public boolean insertQuestion(String que_id,Integer level,String content,String analysis,String point,String cou_id,String cha_id,String que_typ_id,String cou_tar_id);

    /*
     * @Author Tonited
     * @Description //TODO 将题目设置为隐藏
     * @Date 20:49 2020/12/15
     * @Param [questionId]
     * @return boolean
     **/
    @Update("update question set que_access=1 where que_id=#{questionId}")
    public boolean setAccess(String questionId);

}
